package aqr;

public class LinkedListTest {

	public static void main(String[] args) {
		LinkedList linkedList = new LinkedList();
		LinkedList.Node head = linkedList.head();
				
		linkedList.add(new LinkedList.Node("A"));
		linkedList.add(new LinkedList.Node("B"));
		linkedList.add(new LinkedList.Node("C"));
		linkedList.add(new LinkedList.Node("D"));
		linkedList.add(new LinkedList.Node("E"));
		linkedList.add(new LinkedList.Node("F"));
		linkedList.add(new LinkedList.Node("G"));
		linkedList.add(new LinkedList.Node("H"));
		linkedList.add(new LinkedList.Node("I"));
		
		int length = 0;
		LinkedList.Node cur = head;
		LinkedList.Node mid = head;
		
		while (head.next() != null) {
			length++;
			cur = head.next();
			System.out.println("Node #"+length+": "+cur.data());
			if (length%2 == 1) {
				mid = mid.next();
			}
			head = cur;
		}
		
		System.out.println("Middle Node: "+mid.data());
	}
}


class LinkedList {

	private Node head;
	private Node tail;
	
	public LinkedList() {
		head = new Node("head");
		tail = head;
	}
	
	public Node head() {
		return head;
	}
	
	public void add(Node node) {
		tail.next = node;
		tail = node;
	}
	
	public static class Node {
		private Node next;
		private String data;
		
		public Node(String data) {
			this.data = data;
		}
		
		public String data() {
			return data;
		}
		
		public void setData(String data) {
			this.data = data;
		}
		
		public Node next() {
			return next;
		}
		
		public void setNext(Node next) {
			this.next = next;
		}
		
		public String toString() {
			return data;
		}
		
	}
	
}
